package com.ruoyi.bigViw.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bigViw.service.IBigDataViewService;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Service
public class BigDataViewServiceImpl implements IBigDataViewService {

    @Override
    public Map<String, Object> callBigViewThirdPartyApi(Map<String, Object> params) {

        Map<String, Object> mapparams = new HashMap<>();
        mapparams.put("key", params.get("weiboKey"));


        try {
            // String httpurl = "https://whyta.cn/api/tx/bulletin?";
            String httpurl = "https://weibo.com/ajax/side/hotSearch";
            String key = (String) params.get("weiboKey");

            //URL url = new URL(httpurl + "key=" + key );
            URL url = new URL(httpurl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)");


            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            JSONObject jsonObject =  JSON.parseObject(response.toString());

            mapparams.put("weiBoHot", jsonObject);

            System.out.println(response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mapparams;
    }
}
